<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;

class App extends Base
{
    // 获取欢迎词
    public function getWxReplay()
    {
        $content = db('wx_replay')->where('id',1)->value('content');
        apiReturn(200,'获取成功',$content);
    }

    // 修改欢迎词
    public function editWxReplay()
    {
        $data = input();//content
        if (empty($data['content'])) {apiReturn(201,'欢迎词不能为空');}
        db('wx_replay')->where('id',1)->update(['content'=>$data['content']]);
        apiReturn(200,'修改成功');
    }

    // 新增修改问答
    public function appQuestionAdd()
    {
        $data = input();//token id title desc status
        if (empty($data['title'])) {apiReturn(201,'标题不能为空');}
        if (empty($data['desc'])) {apiReturn(201,'回答不能为空');}
        if (!isset($data['id'])) {
            $data['date'] = time();
            db('app_question')->insert($data);
            apiReturn(200,'新增成功');
        }else{
            if (empty($data['id'])) {apiReturn(201,'id不存在');}
            unset($data['token']);
            db('app_question')->where('id',$data['id'])->update($data);
            apiReturn(200,'修改成功');
        }
    }

    // 问答列表
    public function appQuestionList()
    {
        $data = input();//token rows page
        $where = [];
        $list = db('app_question')->where($where)->order('sort_order ASC,date DESC')->limit(($data['page']-1)*$data['rows'],$data['rows'])->select();
        $count = db('app_question')->where($where)->count();
        apiReturn(200,'获取成功',['list'=>$list,'count'=>$count]);
    }

    // 设置问答排序
    public function appQuestionSort()
    {
        $data = input();//token凭证 id问答id sort_order排序

        $exist = db('app_question')->where('id',$data['id'])->find();
        if (empty($exist)) {apiReturn(201,'问答不存在');}
        if ($exist['sort_order'] == $data['sort_order']) {apiReturn(200,'修改排序成功');}
        db('app_question')->where('id',$data['id'])->update($data);
        apiReturn(200,'修改排序成功');
    }

    // 删除问答
    public function appQuestionDelete()
    {
        $data = input();//token凭证 id

        if (empty($data['id'])) {apiReturn(201,'问答id为空');}
        db('app_question')->where('id',$data['id'])->delete();
        apiReturn(200,'删除成功');
    }

    // 作业列表
    public function zuoyeList()
    {
        $data = input();//token goods_next_id子课程集合 client_id name phone starttime endtime rows page

        $where = [];
        $manage_author = $this->getManageAuthor();
        if ($manage_author['is_xiaoshou'] == 1) {
            $where['b.manage_id'] = $manage_author['username'];
        }
        if (isset($data['goods_next_id'])) {
            if (!empty($data['goods_next_id'])) {
                $where['a.goods_next_id'] = ['in',implode(',', $data['goods_next_id'])];
            }
        }
        if (isset($data['client_id'])) {
            if (!empty($data['client_id'])) {
                $where['b.client_id'] = $data['client_id'];
            }
        }
        if (isset($data['name'])) {
            if (!empty($data['name'])) {
                $where['b.name'] = $data['name'];
            }
        }
        if (isset($data['phone'])) {
            if (!empty($data['phone'])) {
                $where['a.phone'] = $data['phone'];
            }
        }
        if (isset($data['starttime']) && isset($data['endtime'])) {
            if (!empty($data['starttime']) && !empty($data['endtime'])) {
                $where['a.create_time'] = array(array('egt',strtotime($data['starttime'])),array('elt',strtotime($data['endtime'])));
            }
        }

        $datas['list'] = db('user_zuoye')->alias('a')->join('tp_crm_customer b','a.user_phone=b.phone')->field('a.*,b.client_id,b.name,b.grade')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('a.create_time DESC')->select();
        $datas['count'] = db('user_zuoye')->alias('a')->join('tp_crm_customer b','a.user_phone=b.phone')->where($where)->count();
        foreach ($datas['list'] as $key => $value) {
            $datas['list'][$key]['goods_next_id'] = db('goods_next')->where('id',$value['goods_next_id'])->value('title');
        }

        apiReturn(200,'获取成功',$datas);
    }

    // 评价列表
    public function evaluateList()
    {
        $data = input();//token client_id name phone starttime endtime rows page

        $where = [];
        $manage_author = $this->getManageAuthor();
        if ($manage_author['is_xiaoshou'] == 1) {
            $where['b.manage_id'] = $manage_author['username'];
        }
        if (isset($data['client_id'])) {
            if (!empty($data['client_id'])) {
                $where['a.client_id'] = $data['client_id'];
            }
        }
        if (isset($data['name'])) {
            if (!empty($data['name'])) {
                $where['b.name'] = $data['name'];
            }
        }
        if (isset($data['phone'])) {
            if (!empty($data['phone'])) {
                $where['b.phone'] = $data['phone'];
            }
        }
        if (isset($data['starttime']) && isset($data['endtime'])) {
            if (!empty($data['starttime']) && !empty($data['endtime'])) {
                $where['a.create_time'] = array(array('egt',strtotime($data['starttime'])),array('elt',strtotime($data['endtime'])));
            }
        }

        $datas['list'] = db('crm_customer_evaluate')->alias('a')->join('tp_crm_customer b','a.client_id=b.client_id')->field('a.*,b.name,b.grade,b.phone')->where($where)->limit(($data['page']-1)*$data['rows'],$data['rows'])->order('a.create_time DESC')->select();
        $datas['count'] = db('crm_customer_evaluate')->alias('a')->join('tp_crm_customer b','a.client_id=b.client_id')->where($where)->count();

        apiReturn(200,'获取成功',$datas);
    }

}
